﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using WebAPILib.APIs.DaZhouDianPing;



namespace MyWebDataSpider
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        ProcessBO processBO = new ProcessBO();
        
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ShopInfo_QueryParams queryParams = CreateQueryParams();

            ShopInfo_Result result = processBO.GetShopInfosByWebAPI(queryParams);

            DisplayResult(result);


        }

        private void DisplayResult(ShopInfo_Result result)
        {
            label1.Text = result.status + "\r\n"
                        + "count:" + result.count.ToString() + "\r\n"
                        + "total_count:" + result.total_count.ToString() + "\r\n";
            listView1.Items.Clear();
            foreach (ShopInfo shop in result.businesses)
            {
                listView1.Items.Add(CreateViewItem(shop));
            }

        }

        private ListViewItem CreateViewItem(ShopInfo shop)
        {
            ListViewItem item = new ListViewItem();
            item.ImageIndex = 0;
            item.Text = shop.business_id.ToString();
            item.Tag = shop;
            item.SubItems.Add(shop.name.ToString());
            item.SubItems.Add(shop.branch_name.ToString());
            item.SubItems.Add(shop.address);
            item.SubItems.Add(shop.categories.ToString());
            item.SubItems.Add(shop.business_url);
            //item.SubItems.Add(shop);
            //item.SubItems.Add(shop);
            //item.SubItems.Add(shop);
            //item.SubItems.Add(shop);
            //item.SubItems.Add(shop);
            //item.SubItems.Add(shop);
            //item.SubItems.Add(shop);
            //item.SubItems.Add(shop);
            //item.SubItems.Add(shop);
            //item.SubItems.Add(shop);
            //item.SubItems.Add(shop);

            return item;
        }


        private ShopInfo_QueryParams CreateQueryParams()
        {
            ShopInfo_QueryParams Params = new ShopInfo_QueryParams();


            Params.city = "武汉";
            Params.limit = 40;
            //Params.city = "武汉";            
            //Params.city = "武汉";
            //Params.city = "武汉";            
            //Params.city = "武汉";
            //Params.city = "武汉";            
            //Params.city = "武汉";
            //Params.city = "武汉";            

            return Params;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (listView1.SelectedItems.Count <= 0) return;
            ShopInfo shop = GetCurrentShopInfo();
            PaintShopInfo(shop);
        }

        private ShopInfo GetCurrentShopInfo()
        {
            ShopInfo shop = listView1.SelectedItems[0].Tag as ShopInfo;
            return shop;
        }

        private void PaintShopInfo(ShopInfo shop)
        {
            if (shop==null)
            {
                label2.Text = "无效数据";
                return;
            }
            string InfoStr = "";
            InfoStr = "\r\n" + "商户名称:" + shop.name
                         + "\r\n" + "地址:" + "\t" + shop.address
                         + "\r\n" + "人均价格:" + "\t" + shop.avg_price.ToString()
                         + "\r\n" + "星级评分:" + "\t" + shop.avg_rating.ToString()
                         + "\r\n" + "分店名称:" + "\t" + shop.branch_name
                         + "\r\n" + "商户ID:" + "\t" + shop.business_id.ToString()
                         + "\r\n" + "商户页面链接:" + "\t" + shop.business_url
                         + "\r\n" + "所属分类信息列表:" + "\t" + shop.categories
                         + "\r\n" + "所在城市:" + "\t" + shop.city
                         + "\r\n" + "优惠劵描述:" + "\t" + shop.coupon_description
                         + "\r\n" + "环境评价:" + "\t" + shop.decoration_grade.ToString()
                         + "\r\n" + "商户与参数坐标的距离:" + "\t" + shop.distance.ToString()
                         + "\r\n" + "纬度坐标:" + "\t" + shop.latitude.ToString()
                         + "\r\n" + "经度坐标:" + "\t" + shop.longitude.ToString()
                         + "\r\n" + "照片链接:" + "\t" + shop.photo_url
                         + "\r\n" + "产品评价:" + "\t" + shop.product_grade.ToString()
                         + "\r\n" + "所在区域信息列表:" + "\t"+ shop.regions
                         + "\r\n" + "点评数量:" + "\t"+ shop.review_count.ToString()
                         + "\r\n" + "小尺寸相片连接:" + "\t"+ shop.s_photo_url
                         + "\r\n" + "服务评价:" + "\t"+ shop.service_grade.ToString()
                         + "\r\n" + "带区号的电话:" + "\t"+ shop.telephone
                        ;

            label2.Text = InfoStr;
            pictureBox1.ImageLocation = shop.s_photo_url;
            pictureBox1.Tag = shop.photo_url;


        }

        Frm_View ViewFrm = new Frm_View();

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            ViewFrm = new Frm_View();
            ViewFrm.SetImageUrl(pictureBox1.Tag.ToString());
            ViewFrm.ShowDialog();
        }

        private void button_Insert_Click(object sender, EventArgs e)
        {
            if (processBO.InsertShopInfo(GetCurrentShopInfo()))
            {
                MessageBox.Show("添加成功");
            }
        }



    }
}
